import 'package:flutter_fitness/model/entity/user.dart';
import 'package:sqflite/sqflite.dart';

import 'DBHelper.dart';

class UserProvider {
  Database db;

  Future open() async {
    db = await DBHelper.instance.open();
  }

  Future<User> insert(User user) async {
    user.id = await db.insert(tableUser, user.toMap());
    return user;
  }

  Future<User> getUser(int id) async {
    List<Map> maps = await db.query(tableUser,
        columns: [columnId, columnUsername],
        where: '$columnId = ?',
        whereArgs: [id]);
    if (maps.length > 0) {
      return User.fromMap(maps.first);
    }
    return null;
  }

  Future<int> update(User user) async {
    return await db.update(tableUser, user.toMap(),
        where: '$columnId = ?', whereArgs: [user.id]);
  }

  Future close() async => db.close();
}
